package com.jh.microservice.rest.controller;

import com.jh.microservice.rest.annotation.OptionsMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CachedController {

    @RequestMapping("/string")
    @ResponseBody
    @OptionsMapping(name = "aaa")
    public String helloWorld() {
        /**
         * 无法缓存，因为无法返回304，前后端无法形成默契
         * 要不是200，要不是500或者400之类的
         */
        return "Hello World!";
    }

    @RequestMapping("/string/cache")
    public ResponseEntity<String> cacheHelloWorld(@RequestParam(required = false, defaultValue = "false") boolean cached) {
        if (cached) {
            return new ResponseEntity<String>(HttpStatus.NOT_MODIFIED);
        } else {
            return ResponseEntity.ok("HelloWorld");
        }
    }
}
